package org.nf.box.service.impl;

import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nf.box.domain.PersonalInfo;
import org.nf.box.domain.User;
import org.nf.box.mapper.UserMapper;
import org.nf.box.service.PersonalInfoService;
import org.nf.box.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author lzp
 * @Date 2024-11-19
 * 用户业务
 */
@Service("userService")
@RequiredArgsConstructor
@Transactional(rollbackFor = RuntimeException.class)
@Slf4j
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;
    private final PersonalInfoService service;

    @Override
    public PageInfo<User> listUser(int pageNum, int pageSize) {
        List<User> list = userMapper.userPageList(pageNum,pageSize);
        return new PageInfo<>(list);
    }

    @Override
    public void addUser(User user) {
        User userById = userMapper.getUserById(user.getUserId());
        User userByAccount = userMapper.getUserByAccount(user.getAccountNumber());
        //如果当前用户不存在
        if (userById == null && userByAccount == null){
            userMapper.addUser(user);
            //添加用户时为用户创建个人资料
            PersonalInfo personalInfo = new PersonalInfo();
            personalInfo.setUserId(user.getUserId());
            personalInfo.setNickName(user.getNickName());
            service.addPointerInfo(personalInfo);
        }
        else {
            throw new RuntimeException("账号存在");
        }
        //log.info("添加用户" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public void deleteUser(int id) {
        userMapper.deleteUser(id);
    }

    @Override
    public User getUserById(Integer userId) {
        return userMapper.getUserById(userId);
    }


}
